package net.zjitc.controller;

import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.APIResponse;
import net.zjitc.utils.JwtTokenUtil;
import net.zjitc.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @ResponseBody
    public APIResponse<Token> login(@RequestBody Admin admin){
        Admin user = adminService.login(admin.getUsername(),admin.getPassword());
        APIResponse<Token> apiResponse = new APIResponse<>();
        apiResponse.setData(new Token());
        if(user!=null){
            String token = JwtTokenUtil.generateToken(admin.getUsername());
            apiResponse.success("登录成功", new Token(token));
            return apiResponse;
        }
        apiResponse.fail(1000,"登录失败", null);
        return apiResponse;
    }
    @GetMapping("/admin")
    public Admin getAdmin() {
        return adminService.getAdmin();
    }
}